# -*- coding: utf-8 -*-
import redis


class RedisClient:
    def __init__(self, host, port, password, *args, **kwargs):
        pool0 = redis.ConnectionPool(host=host, port=port, password=password, db=0, decode_responses=True)
        self.hit_result = redis.Redis(connection_pool=pool0, decode_responses=True)
        pool4 = redis.ConnectionPool(host=host, port=port, password=password, db=4, decode_responses=True)
        self.acv_data = redis.Redis(connection_pool=pool4, decode_responses=True)

        pool6 = redis.ConnectionPool(host=host, port=port, password=password, db=6, decode_responses=True)
        self.card_acvs_data = redis.Redis(connection_pool=pool6, decode_responses=True)

        pool10 = redis.ConnectionPool(host=host, port=port, password=password, db=10, decode_responses=True)
        self.delete_card_data = redis.Redis(connection_pool=pool10, decode_responses=True)

        pool15 = redis.ConnectionPool(host=host, port=port, password=password, db=15, decode_responses=True)
        self.specific_data_pool = redis.Redis(connection_pool=pool15, decode_responses=True)

    # db 0 hit data
    def insert_hit_data(self, list_data):
        self.hit_result.lpush("HIT_RESULT", list_data)

    def insert_hit_run1(self, key, data):
        self.hit_result.lpush(key, data)

    def llen_name(self, name):
        return self.hit_result.llen(name)

    def pop_hit_rin1(self, key):
        v = self.hit_result.rpop(key)
        if v == None:
            return None
        return v

    # 删除人像库
    def del_card_redis(self, card_id):
        self.delete_card_data.sadd("delete", card_id)

    # 获取redis的card_acvs
    def get_card_acvs(self, card_id):
        acvs_list = self.card_acvs_data.zrange(card_id, start=0, end=-1, desc=False)
        return acvs_list

    # db 15 详细信息
    def insert_specific_data(self, name, key, value):
        self.specific_data_pool.hset(name, key, value)

    def get_specific_data(self, name):
        return self.specific_data_pool.hgetall(name)

    def flush_all(self):
        return self.specific_data_pool.flushall()

    def exists_id(self, face_id):
        return self.specific_data_pool.exists(face_id)

    # 贺昌浩
    def has_h_key(self, name, key):
        # true or false
        return self.specific_data_pool.hexists(name, key)

    def RDB_getFace(self, face_image_id):
        # 获取 id 的信息
        face = self.specific_data_pool.hgetall(face_image_id)  # 获取hashd对应的所有键值

        if face['is_front'] == 'True':
            face['is_front'] = True
        if face['is_front'] == 'False':
            face['is_front'] = False

        return face

    def RDB_getfaceacvsall(self, face_id, hit_face_acvs_dic, acvs_list, start=0, end=-1):
        # 由大到小排列
        arr = self.acv_data.zrevrange(face_id, start, end, True)
        for acv_score in arr:
            if int(acv_score[1]) < 300000:
                acvs_list.append(acv_score[0])
                hit_face_acvs_dic[face_id].append(acv_score[0])


# client = redis_client("192.168.1.42", 6379, 12345678)
# print(client.flush_all())
# redis_client("192.168.5.126",6379,None).insert_specific_data("1","test","123")
# client = RedisClient("192.168.1.12", 6379, None)
# print(client.get_specific_data('17443'))
# client.insert_specific_data('11', "set", 12345)
